import 'dart:convert';

import 'package:flutter_deer/database/beans/config/print_style_config.dart';
import 'package:sp_util/sp_util.dart';

import '../../../database/order_status.dart';
import 'order_type_option.dart';

/// 小票样式枚举
class PrintLinkType {
  PrintLinkType(this.code, this.dec, this.linkType);

  static PrintLinkType WORK_CLASS_KNOT_TICKET_CONFIG = PrintLinkType('WORK_CLASS_KNOT_TICKET_CONFIG', '班结小票', 1);

  static PrintLinkType OIL_ORDER_TICKET_CONFIG = PrintLinkType('OIL_ORDER_TICKET_CONFIG', '油品订单小票', 1);
  static PrintLinkType REFUND_OIL_ORDER_TICKET_CONFIG = PrintLinkType('REFUND_OIL_ORDER_TICKET_CONFIG', '油品退款小票', 1);

  static PrintLinkType RECHARGE_VALUE_CARD = PrintLinkType('RECHARGE_VALUE_CARD', '卡充值小票', 1);
  static PrintLinkType REFUND_RECHARGE_VALUE_CARD = PrintLinkType('REFUND_RECHARGE_VALUE_CARD', '卡充值退款小票', 1);

  static PrintLinkType STORE_ORDER = PrintLinkType('STORE_ORDER', '便利店小票', 1);
  static PrintLinkType REFUND_STORE_ORDER = PrintLinkType('REFUND_STORE_ORDER', '便利店退款小票', 1);

  static PrintLinkType GAS_ORDER_TICKET_CONFIG = PrintLinkType('GAS_ORDER_TICKET_CONFIG', 'GAS订单小票', 1);
  static PrintLinkType REFUND_GAS_ORDER_TICKET_CONFIG = PrintLinkType('REFUND_GAS_ORDER_TICKET_CONFIG', 'GAS退款小票', 1);

  static PrintLinkType TIKTOK_TICKET_CONFIG = PrintLinkType('TIKTOK_TICKET_CONFIG', '抖音券核销小票', 1);

  static PrintLinkType AT_WILL_ORDER_TICKET_CONFIG = PrintLinkType('AT_WILL_ORDER_TICKET_CONFIG', '通付订单小票', 1);
  static PrintLinkType REFUND_AT_WILL_ORDER_TICKET_CONFIG = PrintLinkType('REFUND_AT_WILL_ORDER_TICKET_CONFIG', '通付退款小票', 1);

  static PrintLinkType COUPON_BAG_ORDER_TICKET_CONFIG = PrintLinkType('COUPON_BAG_ORDER_TICKET_CONFIG', '付费券包订单小票', 1);
  static PrintLinkType REFUND_COUPON_BAG_ORDER_TICKET_CONFIG = PrintLinkType('REFUND_COUPON_BAG_ORDER_TICKET_CONFIG', '付费券包退款小票', 1);

  static PrintLinkType INTEGRAL_EXCHANGE_ORDER_TICKET_CONFIG = PrintLinkType('INTEGRAL_EXCHANGE_ORDER_TICKET_CONFIG', '积分兑换订单小票', 1);
  static PrintLinkType ENERGY_ORDER_ABANDON_TICKET_CONFIG = PrintLinkType('ENERGY_ORDER_ABANDON_TICKET_CONFIG', '油品弃单小票', 1);
  static PrintLinkType MANUAL_INTEGRATION_TICKET_CONFIG = PrintLinkType('MANUAL_INTEGRATION_TICKET_CONFIG', '手动修改积分小票', 1);
  static PrintLinkType RECHARGE_BALANCE_TICKET_CONFIG = PrintLinkType('RECHARGE_BALANCE_TICKET_CONFIG', '余额打印小票', 1);

  final String code;
  final String dec;
  int linkType;

  static List<PrintLinkType> get values => [
        WORK_CLASS_KNOT_TICKET_CONFIG,
        OIL_ORDER_TICKET_CONFIG,
        REFUND_OIL_ORDER_TICKET_CONFIG,
        RECHARGE_VALUE_CARD,
        REFUND_RECHARGE_VALUE_CARD,
        STORE_ORDER,
        REFUND_STORE_ORDER,
        GAS_ORDER_TICKET_CONFIG,
        REFUND_GAS_ORDER_TICKET_CONFIG,
        TIKTOK_TICKET_CONFIG,
        AT_WILL_ORDER_TICKET_CONFIG,
        REFUND_AT_WILL_ORDER_TICKET_CONFIG,
        COUPON_BAG_ORDER_TICKET_CONFIG,
        REFUND_COUPON_BAG_ORDER_TICKET_CONFIG,
        INTEGRAL_EXCHANGE_ORDER_TICKET_CONFIG,
        ENERGY_ORDER_ABANDON_TICKET_CONFIG,
        MANUAL_INTEGRATION_TICKET_CONFIG,
        RECHARGE_BALANCE_TICKET_CONFIG,
      ];

  static String getDecByCode(String code) {
    for (final value in values) {
      if (value.code == code) {
        return value.dec;
      }
    }
    return '';
  }

  // 根据订单类型获取小票样式
  static PrintLinkType getPrintLinkTypeByOrderType(int orderType, int orderStatus) {
    //是否是退款流程
    final isRefund = orderStatus > OrderPayStatus.FINISH;
    if (orderType == OrderTypeOption.oil.typeId) {
      return isRefund ? REFUND_OIL_ORDER_TICKET_CONFIG : OIL_ORDER_TICKET_CONFIG;
    }
    if (orderType == OrderTypeOption.store.typeId) {
      return isRefund ? REFUND_STORE_ORDER : STORE_ORDER;
    }
    if (orderType == OrderTypeOption.lng.typeId) {
      return isRefund ? REFUND_GAS_ORDER_TICKET_CONFIG : GAS_ORDER_TICKET_CONFIG;
    }
    if (orderType == OrderTypeOption.tongFu.typeId) {
      return isRefund ? REFUND_AT_WILL_ORDER_TICKET_CONFIG : AT_WILL_ORDER_TICKET_CONFIG;
    }
    if (orderType == OrderTypeOption.quanBao.typeId) {
      return isRefund ? REFUND_COUPON_BAG_ORDER_TICKET_CONFIG : COUPON_BAG_ORDER_TICKET_CONFIG;
    }
    if (orderType == OrderTypeOption.recharge.typeId) {
      return isRefund ? REFUND_RECHARGE_VALUE_CARD : RECHARGE_VALUE_CARD;
    }
    if (orderType == OrderTypeOption.integralMall.typeId || orderType == OrderTypeOption.integralUnion.typeId) {
      return INTEGRAL_EXCHANGE_ORDER_TICKET_CONFIG;
    }
    return OIL_ORDER_TICKET_CONFIG;
  }

  static Map<String, PrintStyleConfig> map = {};

  // save
  static void save(String code, PrintStyleConfig linkType) {
    SpUtil.putString(code, linkType.toJson().toString());
    map[code] = linkType;
  }

  // get
  static PrintStyleConfig? getPrintStyle(String code) {
    return map[code] ?? PrintStyleConfig.fromJson(json.decode(SpUtil.getString(code)??'') as Map<String, dynamic>);
  }

  // fromJson 静态方法
  static PrintLinkType fromJson(Map<String, dynamic> json) {
    final String code = json['code'] as String;
    // 通过code查找匹配的枚举值
    for (final value in values) {
      if (value.code == code) {
        value.linkType = json['linkType'] as int;
        return value;
      }
    }
    // 如果没有匹配的枚举值，返回默认值
    return WORK_CLASS_KNOT_TICKET_CONFIG;
  }

  // 转换为JSON的方法
  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'dec': dec,
      'linkType': linkType,
    };
  }

  @override
  bool operator ==(Object other) => identical(this, other) || other is PrintLinkType && runtimeType == other.runtimeType && code == other.code;

  @override
  int get hashCode => code.hashCode;
}

enum PrintCouplet {
  NONE("不打印", 0), //不打印
  CUSTOMER("客户联", 1), //客户联
  MERCHANT("商户联", 2), //商户联
  BOTH_TWO("两联", 3), //两联
  ;

  final String valueCode;
  final int typeCode;

  const PrintCouplet(this.valueCode, this.typeCode);

  static PrintCouplet getPrintCouplet(int typeCode) {
    for (final value in values) {
      if (value.typeCode == typeCode) {
        return value;
      }
    }
    return NONE;
  }
}
